<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/%E6%AD%A6%E6%B1%8932x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/%E6%AD%A6%E6%B1%8916x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib1/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib1/pace/pace-theme-center-simple.min.css">
  <script src="/lib1/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="前言承接之前的webpack的基本配置，这一篇文章主要说明下webpack的性能优化的方法。主要包括两部分：1、优化开发体验，即提升webpack打包时的速度；2、优化输出质量。，即缩小部署到服务器上的源码大小，尽量提升性能。 Webpack优化实践缩小文件范围Loaderloader是一个消耗性能的大户，因此不要使用过多loader，同时缩小loader的查找范围。推荐使用：test、inclu">
<meta property="og:type" content="article">
<meta property="og:title" content="webpack性能优化">
<meta property="og:url" content="http://example.com/2021/03/27/webpack/webpack%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/index.html">
<meta property="og:site_name" content="Technological Blog">
<meta property="og:description" content="前言承接之前的webpack的基本配置，这一篇文章主要说明下webpack的性能优化的方法。主要包括两部分：1、优化开发体验，即提升webpack打包时的速度；2、优化输出质量。，即缩小部署到服务器上的源码大小，尽量提升性能。 Webpack优化实践缩小文件范围Loaderloader是一个消耗性能的大户，因此不要使用过多loader，同时缩小loader的查找范围。推荐使用：test、inclu">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-03-27T04:18:39.000Z">
<meta property="article:modified_time" content="2021-04-02T07:43:37.434Z">
<meta property="article:author" content="Li Yudong">
<meta property="article:tag" content="webpack">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/2021/03/27/webpack/webpack%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>webpack性能优化 | Technological Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Technological Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">IT小白的成长之旅</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="tags fa-fw"></i>标签<span class="badge">18</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="th fa-fw"></i>分类<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="archive fa-fw"></i>归档<span class="badge">95</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/tiarmor1" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/03/27/webpack/webpack%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/author.jpg">
      <meta itemprop="name" content="Li Yudong">
      <meta itemprop="description" content="">
    </span>
    
    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Technological Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          webpack性能优化
        </h1>
    
        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
    
              <time title="创建时间：2021-03-27 12:18:39" itemprop="dateCreated datePublished" datetime="2021-03-27T12:18:39+08:00">2021-03-27</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-04-02 15:43:37" itemprop="dateModified" datetime="2021-04-02T15:43:37+08:00">2021-04-02</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/webpack/" itemprop="url" rel="index"><span itemprop="name">webpack</span></a>
                </span>
            </span>
    
          
    
        </div>
      </header>
    
    
    
    
    <div class="post-body" itemprop="articleBody">
    
      
        <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>承接之前的webpack的基本配置，这一篇文章主要说明下webpack的性能优化的方法。主要包括两部分：1、优化开发体验，即提升webpack打包时的速度；2、优化输出质量。，即缩小部署到服务器上的源码大小，尽量提升性能。</p>
<h1 id="Webpack优化实践"><a href="#Webpack优化实践" class="headerlink" title="Webpack优化实践"></a>Webpack优化实践</h1><h2 id="缩小文件范围Loader"><a href="#缩小文件范围Loader" class="headerlink" title="缩小文件范围Loader"></a>缩小文件范围Loader</h2><p>loader是一个消耗性能的大户，因此不要使用过多loader，同时缩小loader的查找范围。推荐使用：test、include、exclude来缩小处理范围。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">include: path.resolve(__dirname, <span class="string">"./src"</span>),</span><br></pre></td></tr></table></figure>

<h2 id="优化resolve配置"><a href="#优化resolve配置" class="headerlink" title="优化resolve配置"></a>优化resolve配置</h2><h3 id="优化resolve-modules配置"><a href="#优化resolve-modules配置" class="headerlink" title="优化resolve.modules配置"></a>优化resolve.modules配置</h3><p>resolve.modules⽤于配置webpack去哪些⽬录下寻找第三⽅模块。默认是在当前项⽬⽬录下的node_modules⾥⾯去找，如果没有找到，就会去上⼀级⽬录../node_modules找，再没有会去../../node_modules中找，以此类推，和Node.js的模块寻找机制很类似。如果我们的第三⽅模块都安装在了项⽬根⽬录下，就可以直接指明这个路径</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports=&#123;</span><br><span class="line"> resolve:&#123;</span><br><span class="line"> modules: [path.resolve(__dirname, <span class="string">"./node_modules"</span>)]</span><br><span class="line"> &#125; &#125;</span><br></pre></td></tr></table></figure>

<h3 id="优化resolve-alias配置"><a href="#优化resolve-alias配置" class="headerlink" title="优化resolve.alias配置"></a>优化<strong>resolve.alias</strong>配置</h3><p>resolve.alias配置通过别名来将原导⼊路径映射成⼀个新的导⼊路径，拿react为例，我们引⼊的react库，⼀般存在两套代码。</p>
<p>1、CJS：采用commonJS规范的模块化代码；2、UMD：已经打包好的完整代码，没有采⽤模块化，可以直接执⾏。</p>
<p>默认情况下，webpack会从⼊⼝⽂件./node_modules/bin/react/index开始递归解析和处理依赖的⽂件。我们可以直接指定⽂件，避免这处的耗时。其实就是减少查找过程、起别名。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">alias: &#123;</span><br><span class="line">    <span class="string">"@"</span>: path.join(__dirname, <span class="string">"./pages"</span>),</span><br><span class="line">        react: path.resolve(</span><br><span class="line">            __dirname,</span><br><span class="line">            <span class="string">"./node_modules/react/umd/react.production.min.js"</span></span><br><span class="line">        ),</span><br><span class="line">            <span class="string">"react-dom"</span>: path.resolve(</span><br><span class="line">                __dirname,</span><br><span class="line">                <span class="string">"./node_modules/react-dom/umd/react-dom.production.min.js"</span></span><br><span class="line">            )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="优化resolve-extensions配置"><a href="#优化resolve-extensions配置" class="headerlink" title="优化resolve.extensions配置"></a>优化<strong>resolve.extensions</strong>配置</h3><p>resolve.extensions在导⼊语句没带⽂件后缀时，webpack会⾃动带上后缀后，去尝试查找⽂件是否存在。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">extensions:[<span class="string">'.js'</span>,<span class="string">'.json'</span>,<span class="string">'.jsx'</span>,<span class="string">'.ts'</span>]</span><br><span class="line"><span class="comment">//后缀尝试列表尽量的小、导入语句尽量的带上后缀</span></span><br></pre></td></tr></table></figure>

<h2 id="基于CDN的优化"><a href="#基于CDN的优化" class="headerlink" title="基于CDN的优化"></a>基于CDN的优化</h2><h3 id="使⽤externals优化cdn静态资源"><a href="#使⽤externals优化cdn静态资源" class="headerlink" title="使⽤externals优化cdn静态资源"></a>使⽤<strong>externals</strong>优化<strong>cdn</strong>静态资源</h3><p>我们可以将⼀些JS⽂件存储在 CDN 上(减少 Webpack 打包出来的 js 体积)，在 index.html 中通过标签引⼊。我们希望在使⽤时，仍然可以通过 import 的⽅式去引⽤(如 import $ from ‘jquery’ )，并且希望webpack 不会对其进⾏打包，此时就可以配置 externals 。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//webpack.config.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line"> <span class="comment">//...</span></span><br><span class="line"> externals: &#123;</span><br><span class="line">     <span class="comment">//jquery通过script引⼊之后，全局中即有了 jQuery 变量</span></span><br><span class="line">     <span class="string">'jquery'</span>: <span class="string">'jQuery'</span></span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>为什么全局引入了还要webpack引入：因为你在写代码的时候不引入jQuery模块，就会报错。html模板上添加CDN资源，配置上面externals忽略CDN资源。但此时，打包后的资源就不需要再打包进去了。</p>
<h3 id="使⽤静态资源路径publicPath-CDN"><a href="#使⽤静态资源路径publicPath-CDN" class="headerlink" title="使⽤静态资源路径publicPath(CDN)"></a>使⽤静态资源路径publicPath(CDN)</h3><p>CDN通过将资源部署到世界各地，使得⽤户可以就近访问资源，加快访问速度。要接⼊CDN，需要把⽹⻚的静态资源上传到CDN服务上，在访问这些资源时，使⽤CDN服务提供的URL</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">##webpack.config.js</span><br><span class="line">output:&#123;</span><br><span class="line"> publicPath: <span class="string">'//cdnURL.com'</span>, <span class="comment">//指定存放JS⽂件的CDN地址</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="CSS、HTML的处理"><a href="#CSS、HTML的处理" class="headerlink" title="CSS、HTML的处理"></a>CSS、HTML的处理</h2><h3 id="CSS处理"><a href="#CSS处理" class="headerlink" title="CSS处理"></a>CSS处理</h3><p>1、使⽤less或者sass当做css技术栈；2、使⽤postcss为样式⾃动补⻬浏览器前缀；</p>
<p>如果不做抽取配置，我们的 css 是直接打包进 js ⾥⾯的，我们希望能单独⽣成 css ⽂件。 因为单独⽣成css,css可以和js并⾏下载，提⾼⻚⾯加载效率</p>
<h4 id="借助MiniCssExtractPlugin-完成抽离css"><a href="#借助MiniCssExtractPlugin-完成抽离css" class="headerlink" title="借助MiniCssExtractPlugin 完成抽离css"></a>借助<strong>MiniCssExtractPlugin</strong> 完成抽离<strong>css</strong></h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> MiniCssExtractPlugin = <span class="built_in">require</span>(<span class="string">"mini-css-extract-plugin"</span>);</span><br><span class="line"> &#123;</span><br><span class="line">     test: <span class="regexp">/\.scss$/</span>,</span><br><span class="line">     use: [</span><br><span class="line"> <span class="comment">// "style-loader", // 不再需要style-loader，⽤MiniCssExtractPlugin.loader代替</span></span><br><span class="line"> MiniCssExtractPlugin.loader,</span><br><span class="line"> <span class="string">"css-loader"</span>, <span class="comment">// 编译css</span></span><br><span class="line"> <span class="string">"postcss-loader"</span>,</span><br><span class="line"> <span class="string">"sass-loader"</span> <span class="comment">// 编译scss</span></span><br><span class="line">     ]</span><br><span class="line"> &#125;,</span><br><span class="line">plugins: [</span><br><span class="line">    <span class="keyword">new</span> MiniCssExtractPlugin(&#123;</span><br><span class="line">        filename: <span class="string">"css/[name]_[contenthash:6].css"</span>,</span><br><span class="line">        chunkFilename: <span class="string">"[id].css"</span></span><br><span class="line">    &#125;)</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<h4 id="压缩CSS"><a href="#压缩CSS" class="headerlink" title="压缩CSS"></a>压缩CSS</h4><p>借助 optimize-css-assets-webpack-plugin；借助cssnano</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> OptimizeCSSAssetsPlugin = <span class="built_in">require</span>(<span class="string">"optimize-css-assets-webpack-plugin"</span>);</span><br><span class="line"><span class="keyword">new</span> OptimizeCSSAssetsPlugin(&#123;</span><br><span class="line"> cssProcessor: <span class="built_in">require</span>(<span class="string">"cssnano"</span>), <span class="comment">//引⼊cssnano配置压缩选项</span></span><br><span class="line"> cssProcessorOptions: &#123;</span><br><span class="line"> discardComments: &#123; <span class="attr">removeAll</span>: <span class="literal">true</span> &#125;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="HTML处理"><a href="#HTML处理" class="headerlink" title="HTML处理"></a>HTML处理</h3><p>借助html-webpack-plugin</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> htmlWebpackPlugin(&#123;</span><br><span class="line">    title: <span class="string">"京东商城"</span>,</span><br><span class="line">    template: <span class="string">"./index.html"</span>,</span><br><span class="line">    filename: <span class="string">"index.html"</span>,</span><br><span class="line">    minify: &#123;</span><br><span class="line">        <span class="comment">// 压缩HTML⽂件</span></span><br><span class="line">        removeComments: <span class="literal">true</span>, <span class="comment">// 移除HTML中的注释</span></span><br><span class="line">        collapseWhitespace: <span class="literal">true</span>, <span class="comment">// 删除空⽩符与换⾏符</span></span><br><span class="line">        minifyCSS: <span class="literal">true</span> <span class="comment">// 压缩内联css</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;),</span><br></pre></td></tr></table></figure>

<h2 id="开发、生产模式区分"><a href="#开发、生产模式区分" class="headerlink" title="开发、生产模式区分"></a>开发、生产模式区分</h2><p>利用webpack-merge来进行合并，</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> merge = <span class="built_in">require</span>(<span class="string">"webpack-merge"</span>)</span><br><span class="line"><span class="keyword">const</span> commonConfig = <span class="built_in">require</span>(<span class="string">"./webpack.common.js"</span>)</span><br><span class="line"><span class="keyword">const</span> devConfig = &#123;</span><br><span class="line"> ...</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">module</span>.exports = merge(commonConfig,devConfig)</span><br><span class="line"><span class="comment">//package.js</span></span><br><span class="line"><span class="string">"scripts"</span>:&#123;</span><br><span class="line"> <span class="string">"dev"</span>:<span class="string">"webpack-dev-server --config ./build/webpack.dev.js"</span>,</span><br><span class="line"> <span class="string">"build"</span>:<span class="string">"webpack --config ./build/webpack.prod.js"</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>借助cross-env；package⾥⾯配置命令脚本，传⼊参数；在webpack.confifig.js⾥拿到参数</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//外部传⼊的全局变量</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function">(<span class="params">env</span>)=&gt;</span>&#123;</span><br><span class="line"> <span class="keyword">if</span>(env &amp;&amp; env.production)&#123;</span><br><span class="line"> <span class="keyword">return</span> merge(commonConfig,prodConfig)</span><br><span class="line"> &#125;<span class="keyword">else</span>&#123;</span><br><span class="line"> <span class="keyword">return</span> merge(commonConfig,devConfig)</span><br><span class="line"> &#125; &#125;</span><br><span class="line"><span class="comment">//外部传⼊变量</span></span><br><span class="line">scripts:<span class="string">" --env.production"</span></span><br></pre></td></tr></table></figure>

<h2 id="tree-shaking"><a href="#tree-shaking" class="headerlink" title="tree-shaking"></a>tree-shaking</h2><p>webpack2.x开始⽀持 tree shaking概念，顾名思义，”摇树”，清除⽆⽤ css,js(Dead Code)。主要分为CSS tree shaking和JS tree shaking。</p>
<p>Dead Code ⼀般具有以下⼏个特征：1、代码不会被执⾏，不可到达；2、代码执⾏的结果不会被⽤到；3、代码只会影响死变量（只写不读）4、Js tree shaking只⽀持ES module的引⼊⽅式！！！！,</p>
<h3 id="CSS-tree-shaking"><a href="#CSS-tree-shaking" class="headerlink" title="CSS tree shaking"></a>CSS tree shaking</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">npm i glob-all purify-css purifycss-webpack --save-dev</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> PurifyCSS = <span class="built_in">require</span>(<span class="string">'purifycss-webpack'</span>)</span><br><span class="line"><span class="keyword">const</span> glob = <span class="built_in">require</span>(<span class="string">'glob-all'</span>)</span><br><span class="line">plugins:[</span><br><span class="line"> <span class="comment">// 清除⽆⽤ css</span></span><br><span class="line"> <span class="keyword">new</span> PurifyCSS(&#123;</span><br><span class="line"> paths: glob.sync([</span><br><span class="line"> <span class="comment">// 要做 CSS Tree Shaking 的路径⽂件</span></span><br><span class="line"> path.resolve(__dirname, <span class="string">'./src/*.html'</span>), <span class="comment">// 请注意，我们同样需要对 html ⽂件进⾏ tree shaking</span></span><br><span class="line"> path.resolve(__dirname, <span class="string">'./src/*.js'</span>)</span><br><span class="line"> ])</span><br><span class="line"> &#125;)</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<h3 id="JS-tree-shaking"><a href="#JS-tree-shaking" class="headerlink" title="JS tree shaking"></a>JS tree shaking</h3><p>只⽀持import⽅式引⼊，不⽀持commonjs的⽅式引⼊</p>
<p>只要<strong>mode</strong>是<strong>production</strong>就会⽣效，<strong>develpoment</strong>的<strong>tree shaking</strong>是不⽣效的，因为<strong>webpack</strong>为了⽅便你的调试。可以查看打包后的代码注释以辨别是否⽣效。⽣产模式不需要配置，默认开启</p>
<h3 id="副作用"><a href="#副作用" class="headerlink" title="副作用"></a>副作用</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//package.json</span></span><br><span class="line"><span class="string">"sideEffects"</span>:<span class="literal">false</span> <span class="comment">//正常对所有模块进⾏tree shaking , 仅⽣产模式有效，需要配合usedExports</span></span><br><span class="line"><span class="comment">//或者在数组⾥⾯排除不需要tree shaking的模块</span></span><br><span class="line"><span class="string">"sideEffects"</span>:[<span class="string">'*.css'</span>,<span class="string">'@babel/polyfill'</span>]</span><br></pre></td></tr></table></figure>

<h2 id="代码分割code-Splitting"><a href="#代码分割code-Splitting" class="headerlink" title="代码分割code Splitting"></a>代码分割code Splitting</h2><p>单页面应用SPA：打包完后，所有⻚⾯只⽣成了⼀个bundle.js。1、代码体积变⼤，不利于下载；2、没有合理利⽤浏览器资源。因为浏览器可做多支持同时5个请求加载资源。</p>
<p>多⻚⾯应⽤MPA：如果多个⻚⾯引⼊了⼀些公共模块，那么可以把这些公共的模块抽离出来，单独打包。公共代码只需要下载⼀次就缓存起来了，避免了重复下载。</p>
<p>假如我们引⼊⼀个第三⽅的⼯具库，体积为1mb，⽽我们的业务逻辑代码也有1mb，那么打包出来的体积⼤⼩会在2mb。导致问题：1、体积⼤，加载时间⻓；2、业务逻辑会变化，第三⽅⼯具库不会，所以业务逻辑⼀变更，第三⽅⼯具库也要跟着变</p>
<p>其实code Splitting概念 与 webpack并没有直接的关系，只不过webpack中提供了⼀种更加⽅便的⽅法供我们实现代码分割，基于split-chunks-plugin，我们可以实现简单的代码分割，让浏览器能更快速地加载代码。 </p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">optimization: &#123;</span><br><span class="line">    splitChunks: &#123;</span><br><span class="line">        chunks: <span class="string">"all"</span>, <span class="comment">// 所有的 chunks 代码公共的部分分离出来成为⼀个单独的⽂件</span></span><br><span class="line">            <span class="comment">//即支持同步引入的模块，也支持异步引入的模块</span></span><br><span class="line">    &#125;,</span><br><span class="line">&#125;,</span><br><span class="line"><span class="comment">//optimization的配置如下：</span></span><br><span class="line">optimization: &#123;</span><br><span class="line"> splitChunks: &#123;</span><br><span class="line">     chunks: <span class="string">'async'</span>,<span class="comment">//对同步 initial，异步 async，所有的模块有效 all</span></span><br><span class="line">     minSize: <span class="number">30000</span>,<span class="comment">//最⼩尺⼨，当模块⼤于30kb</span></span><br><span class="line">     maxSize: <span class="number">0</span>,<span class="comment">//对模块进⾏⼆次分割时使⽤，不推荐使⽤</span></span><br><span class="line">     minChunks: <span class="number">1</span>,<span class="comment">//打包⽣成的chunk⽂件最少有⼏个chunk引⽤了这个模块</span></span><br><span class="line">     maxAsyncRequests: <span class="number">5</span>,<span class="comment">//最⼤异步请求数，默认5</span></span><br><span class="line">     maxInitialRequests: <span class="number">3</span>,<span class="comment">//最⼤初始化请求书，⼊⼝⽂件同步请求，默认3</span></span><br><span class="line">     automaticNameDelimiter: <span class="string">'-'</span>,<span class="comment">//打包分割符号</span></span><br><span class="line">     name: <span class="literal">true</span>,<span class="comment">//打包后的名称，除了布尔值，还可以接收⼀个函数function</span></span><br><span class="line">     cacheGroups: &#123;<span class="comment">//缓存组</span></span><br><span class="line">     vendors: &#123;</span><br><span class="line">         test: <span class="regexp">/[\\/]node_modules[\\/]/</span>,</span><br><span class="line">         name:<span class="string">"vendor"</span>, <span class="comment">// 要缓存的 分隔出来的 chunk 名称</span></span><br><span class="line">         priority: <span class="number">-10</span><span class="comment">//缓存组优先级 数字越⼤，优先级越⾼</span></span><br><span class="line">     &#125;,</span><br><span class="line"> other:&#123;</span><br><span class="line">     chunks: <span class="string">"initial"</span>, <span class="comment">// 必须三选⼀： "initial" | "all" | "async"(默认就是async)</span></span><br><span class="line">     test: <span class="regexp">/react|lodash/</span>, <span class="comment">// 正则规则验证，如果符合就提取 chunk,</span></span><br><span class="line">     name:<span class="string">"other"</span>,</span><br><span class="line">     minSize: <span class="number">30000</span>,</span><br><span class="line">     minChunks: <span class="number">1</span>,</span><br><span class="line"> &#125;,</span><br><span class="line"> <span class="keyword">default</span>: &#123;</span><br><span class="line">     minChunks: <span class="number">2</span>,</span><br><span class="line">     priority: <span class="number">-20</span>,</span><br><span class="line">     reuseExistingChunk: <span class="literal">true</span><span class="comment">//可设置是否重⽤该chunk</span></span><br><span class="line">     &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>

<h2 id="Scope-Hoisting"><a href="#Scope-Hoisting" class="headerlink" title="Scope Hoisting"></a><strong>Scope Hoisting</strong></h2><p>作⽤域提升（Scope Hoisting）是指 webpack 通过 ES6 语法的静态分析，分析出模块之间的依赖关系，尽可能地把模块放到同⼀个函数中。过配置 optimization：开启 Scope Hoisting，通过 Scope Hoisting 的功能可以让Webpack 打包出来的代码⽂件更⼩、运⾏的更快。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    optimization: &#123;</span><br><span class="line">        concatenateModules: <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="使⽤⼯具量化"><a href="#使⽤⼯具量化" class="headerlink" title="使⽤⼯具量化"></a>使⽤⼯具量化</h2><p>1、speed-measure-webpack-plugin:可以测量各个插件和 loader 所花费的时间</p>
<p>2、webpack-bundle-analyzer:分析webpack打包后的模块依赖关系：vv</p>
<h2 id="DllPlugin插件打包第三⽅类库"><a href="#DllPlugin插件打包第三⽅类库" class="headerlink" title="DllPlugin插件打包第三⽅类库"></a><strong>DllPlugin</strong>插件打包第三⽅类库</h2><p>每次打包，所有的依赖都需要重新构建一次，但有些公共的框架不需要每次重新构建，这样我们就可以先对这些不怎么改变的框架代码进行缓存，这样我们构建一个缓存的动态信息库。每次构建并检查依赖时，如果已经有了对应的动态缓存，就不再次进行构建，而是直接从缓存中查找，就可以减少第二次与以后的缓存时间。</p>
<p>Dll动态链接库 其实就是做缓存，只会提升webpack打包的速度，但并不会减少生成的代码体积，因此只是减少了开发时候的体验。项⽬中引⼊了很多第三⽅库，这些库在很⻓的⼀段时间内，基本不会更新，打包的时候分开打包来提升打包速度，⽽DllPlugin动态链接库插件，其原理就是把⽹⻚依赖的基础模块抽离出来打包到<strong>dll</strong>⽂件中，当需要导⼊的模块存在于某个<strong>dll</strong>中时，这个模块不再被打包，⽽是去<strong>dll</strong>中获取。</p>
<p>webpack已经内置了对动态链接库的⽀持。1、DllPlugin:⽤于打包出⼀个个单独的动态链接库⽂件。2、DllReferencePlugin：⽤于在主要的配置⽂件中引⼊DllPlugin插件打包好的动态链接库⽂件</p>
<p>新建webpack.dll.confifig.js⽂件，打包基础模块；你会发现多了⼀个dll⽂件夹，⾥边有dll.js⽂件，这样我们就把我们的React这些已经单独打包了。1、dll⽂件包含了⼤量模块的代码，这些模块被存放在⼀个数组⾥。⽤数组的索引号为ID,通过变量将⾃⼰暴露在全局中，就可以在window.xxx访问到其中的模块。2、Manifest.json 描述了与其对应的dll.js包含了哪些模块，以及ID和路径。</p>
<p>接下来要如何去使用：大概是要给web项目构建介入动态链接库，需要完成以下步骤：</p>
<p>1、将⽹⻚依赖的基础模块抽离，打包到单独的动态链接库，⼀个动态链接库是可以包含多个模块的。</p>
<p>2、当需要导⼊的模块存在于某个动态链接库中时，不要再次打包，直接使⽤构建好的动态链接库即可。</p>
<p>3、⻚⾯依赖的所有动态链接库都需要被加载。</p>
<p>这里其实可以使用add-asset-html-webpack-plugin插件来实现添加使用它会将我们打包后的 dll.js ⽂件注⼊到我们⽣成的 index.html 中.在 webpack.base.confifig.js ⽂件中进⾏更改。</p>
<p>但其实上述的操作执行起来比较麻烦，在webpack5上面已经有了<strong>HardSourceWebpackPlugin</strong>插件来代替实现共功能。1、提供中间缓存的作⽤；2、⾸次构建没有太⼤的变化；3、第⼆次构建时间就会有较⼤的节省</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> HardSourceWebpackPlugin = <span class="built_in">require</span>(<span class="string">'hard-source-webpack-plugin'</span>)</span><br><span class="line"><span class="keyword">const</span> plugins = [</span><br><span class="line"> snew HardSourceWebpackPlugin()</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<h2 id="使⽤happypack并发执⾏任务"><a href="#使⽤happypack并发执⾏任务" class="headerlink" title="使⽤happypack并发执⾏任务"></a>使⽤<strong>happypack</strong>并发执⾏任务</h2><p>运⾏在 Node.之上的Webpack是单线程模型的，也就是说Webpack需要⼀个⼀个地处理任务，不能同时处理多个任务。 <strong>Happy Pack</strong> 就能让Webpack做到这⼀点，它将任务分解给多个⼦进程去并发执⾏，⼦进程处理完后再将结果发送给主进程。从⽽发挥多核 CPU 电脑的威⼒。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">npm i -D happypack</span><br><span class="line"><span class="keyword">var</span> happyThreadPool = HappyPack.ThreadPool(&#123; <span class="attr">size</span>: <span class="number">5</span> &#125;);</span><br><span class="line"> <span class="comment">//const happyThreadPool = HappyPack.ThreadPool(&#123; size: os.cpus().length &#125;)</span></span><br><span class="line"><span class="comment">// webpack.config.js</span></span><br><span class="line">rules: [</span><br><span class="line">    &#123;</span><br><span class="line">        test: <span class="regexp">/\.jsx?$/</span>,</span><br><span class="line">        exclude: <span class="regexp">/node_modules/</span>,</span><br><span class="line">        use: [</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="comment">// ⼀个loader对应⼀个id</span></span><br><span class="line">                loader: <span class="string">"happypack/loader?id=babel"</span></span><br><span class="line">            &#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">        test: <span class="regexp">/\.css$/</span>,</span><br><span class="line">        include: path.resolve(__dirname, <span class="string">"./src"</span>),</span><br><span class="line">        use: [<span class="string">"happypack/loader?id=css"</span>]</span><br><span class="line">    &#125;,</span><br><span class="line">]</span><br><span class="line"><span class="comment">//在plugins中增加</span></span><br><span class="line">plugins:[</span><br><span class="line">    <span class="keyword">new</span> HappyPack(&#123;</span><br><span class="line">        <span class="comment">// ⽤唯⼀的标识符id，来代表当前的HappyPack是⽤来处理⼀类特定的⽂件</span></span><br><span class="line">        id:<span class="string">'babel'</span>,</span><br><span class="line">        <span class="comment">// 如何处理.js⽂件，⽤法和Loader配置中⼀样</span></span><br><span class="line">        loaders:[<span class="string">'babel-loader?cacheDirectory'</span>]</span><br><span class="line">        threadPool: happyThreadPool,</span><br><span class="line">    &#125;),</span><br><span class="line">    <span class="keyword">new</span> HappyPack(&#123;</span><br><span class="line">        id: <span class="string">"css"</span>,</span><br><span class="line">        loaders: [<span class="string">"style-loader"</span>, <span class="string">"css-loader"</span>]</span><br><span class="line">    &#125;),</span><br><span class="line">]</span><br></pre></td></tr></table></figure>


    </div>
    
    
    
    
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>
        <div class="reward-container">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/alipay.png" alt="Li Yudong 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Li Yudong
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://example.com/2021/03/27/webpack/webpack%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" title="webpack性能优化">http://example.com/2021/03/27/webpack/webpack性能优化/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

    
      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/webpack/" rel="tag"># webpack</a>
          </div>
    
        

    
        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/03/21/webpack/webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE/" rel="prev" title="webpack基础配置">
      <i class="fa fa-chevron-left"></i> webpack基础配置
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/04/02/webpack/webpack%E5%8E%9F%E7%90%86/" rel="next" title="webpack原理分析">
      webpack原理分析 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Webpack优化实践"><span class="nav-number">2.</span> <span class="nav-text">Webpack优化实践</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#缩小文件范围Loader"><span class="nav-number">2.1.</span> <span class="nav-text">缩小文件范围Loader</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#优化resolve配置"><span class="nav-number">2.2.</span> <span class="nav-text">优化resolve配置</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#优化resolve-modules配置"><span class="nav-number">2.2.1.</span> <span class="nav-text">优化resolve.modules配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#优化resolve-alias配置"><span class="nav-number">2.2.2.</span> <span class="nav-text">优化resolve.alias配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#优化resolve-extensions配置"><span class="nav-number">2.2.3.</span> <span class="nav-text">优化resolve.extensions配置</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#基于CDN的优化"><span class="nav-number">2.3.</span> <span class="nav-text">基于CDN的优化</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#使⽤externals优化cdn静态资源"><span class="nav-number">2.3.1.</span> <span class="nav-text">使⽤externals优化cdn静态资源</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使⽤静态资源路径publicPath-CDN"><span class="nav-number">2.3.2.</span> <span class="nav-text">使⽤静态资源路径publicPath(CDN)</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#CSS、HTML的处理"><span class="nav-number">2.4.</span> <span class="nav-text">CSS、HTML的处理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#CSS处理"><span class="nav-number">2.4.1.</span> <span class="nav-text">CSS处理</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#借助MiniCssExtractPlugin-完成抽离css"><span class="nav-number">2.4.1.1.</span> <span class="nav-text">借助MiniCssExtractPlugin 完成抽离css</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#压缩CSS"><span class="nav-number">2.4.1.2.</span> <span class="nav-text">压缩CSS</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#HTML处理"><span class="nav-number">2.4.2.</span> <span class="nav-text">HTML处理</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#开发、生产模式区分"><span class="nav-number">2.5.</span> <span class="nav-text">开发、生产模式区分</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#tree-shaking"><span class="nav-number">2.6.</span> <span class="nav-text">tree-shaking</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#CSS-tree-shaking"><span class="nav-number">2.6.1.</span> <span class="nav-text">CSS tree shaking</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#JS-tree-shaking"><span class="nav-number">2.6.2.</span> <span class="nav-text">JS tree shaking</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#副作用"><span class="nav-number">2.6.3.</span> <span class="nav-text">副作用</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#代码分割code-Splitting"><span class="nav-number">2.7.</span> <span class="nav-text">代码分割code Splitting</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Scope-Hoisting"><span class="nav-number">2.8.</span> <span class="nav-text">Scope Hoisting</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使⽤⼯具量化"><span class="nav-number">2.9.</span> <span class="nav-text">使⽤⼯具量化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#DllPlugin插件打包第三⽅类库"><span class="nav-number">2.10.</span> <span class="nav-text">DllPlugin插件打包第三⽅类库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使⽤happypack并发执⾏任务"><span class="nav-number">2.11.</span> <span class="nav-text">使⽤happypack并发执⾏任务</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Li Yudong"
      src="/images/author.jpg">
  <p class="site-author-name" itemprop="name">Li Yudong</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">95</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/tiarmor1" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;tiarmor1" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1157019137@qq.com" title="E-Mail → mailto:1157019137@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2020 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Li Yudong</span>
</div>

        








      </div>
    </footer>
  </div>

  
  
  <script color='0,0,0' opacity='0.5' zIndex='-1' count='150' src="/lib1/canvas-nest/canvas-nest.min.js"></script>
  <script src="/lib1/anime.min.js"></script>
  <script src="/lib1/velocity/velocity.min.js"></script>
  <script src="/lib1/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

  
   <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;" ></canvas> 
   <script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
   <script type="text/javascript" src="/js/fireworks.js"></script>
  
</body>
</html>

